<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/25
 * Time: 9:18
 */

namespace app\portal\controller;
use cmf\phpqrcode\QRcode;
use think\Db;

class TestController
{
    //更新打卡测试
    public function update_sign(){
        $now=time();
        $user_id=29;
        $data=Db::name('activity_log')->where('user_id',$user_id)->find();
        $duration=$now-$data['start_time'];
        Db::name('activity_log')->where('user_id',29)->update([
            'end_time'=>$now,
            'duration' => $duration
        ]);
        $user_rank=Db::name('user_rank')->where('user_id',$user_id)->find();
        $points=$duration/3600*2;
        if(!empty($data['end_time'])){
            echo '更新签退~';
            $user_rank['duration']=$user_rank['duration']+$duration-$data['duration'];
            $user_rank['all_points']=$user_rank['all_points']+ $points-$data['duration']/3600*2;
            $user_rank['can_points']=$user_rank['can_points']+ $points-$data['duration']/3600*2;
            Db::name('user_rank')->update($user_rank);
        }else{
            echo '首次签退~';
            $user_rank['duration']=$user_rank['duration']+$duration;
            $user_rank['q_times']=$user_rank['q_times']+1;
            $user_rank['all_points']=$user_rank['all_points']+ $points;
            $user_rank['can_points']=$user_rank['can_points']+ $points;
            Db::name('user_rank')->update($user_rank);
        }

    }
    public function get(){

        $openid=session('openid');
        $user=session('user');
        dump($openid);
        dump($user);

    }
    public function set_openid_null(){
        session('openid',null);
    }
    public function set_user_null(){
        session('user',null);
    }
    //删除测试用户
    public function del_user(){
        Db::name('user')->where('mobile',13386330861)->delete();
    }
    public function test(){
        $a=cmf_get_current_user_id();
        dump($a);
    }
    //抓取志愿者人员信息
    public function index(){
        set_time_limit(0);
//        for ($i=1;$i<=183;$i++){
//            $text=file_get_contents('http://rzqianshou.com/honor.asp?page='.$i);
//            $text = mb_convert_encoding($text,'UTF-8','GBK');
//            preg_match_all('/id="hh">姓名：(.*?)<\/a><\/td>
//		   <td width="20%">编号：(.*?)<\/td>
//		  <td width="20%">服务时间：(.*?)<\/td>
//		  <td width="30%">加入时间：(.*?)<\/td>/',$text,$match);
//            for($j=0;$j<count($match[0]);$j++){
//                $data=['name'=>$match[1][$j],'number'=>$match[2][$j],'timelen'=>$match[3][$j]];
//                Db::name('test_data')->insert($data);
//            }
//        }
    }
    //生成二维码
    public function qrcode($value='defalt'){
        $errorCorrectionLevel = 'L';
        $matrixPointSize = 5;
        $filename = 'upload/qrcode/'.uniqid().'.png';
         QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
        $QR = $filename;        //已经生成的原始二维码图片文件
        //echo $QR;
        $QR = imagecreatefromstring(file_get_contents($QR));
        //输出图片
        imagepng($QR);
        imagedestroy($QR);
//        return '<img src=/'.$filename.'alt="使用微信扫描支付">';
    }
    //创建活动流程及注意细节
    //时间限制 地点限制 人物身份限制
    public function do_activity(){

    }

    function twoSum($nums, $target) {
        foreach ($nums as $k=>$v){
            foreach ($nums as $ke=>$va){
                if($v+$va==$target&&$k!=$ke){
                    return $arr=[$k,$ke];
                }
            }
        }

    }

    function do_it(){
        $nums = [2, 7, 11, 15];
        $target = 9;
        $res=$this->twoSum($nums,$target);
        dump($res);
    }


}